﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WeiXin.SDK.Entities;

namespace WeiXin.SDK
{
    public class UserAdminAPI
    {

        /// <summary>
        /// 创建分组
        /// 注意：一个公众账号，最多支持创建500个分组
        /// </summary>
        /// <param name="access_token">调用接口凭证</param>
        /// <param name="name">分组名字（30个字符以内）</param>
        /// <returns></returns>
        public static bool CreateGroup(string access_token, string name)
        {
            var builder = new StringBuilder();
            builder.Append("{")
                .Append('"' + "group" + '"' + ":")
                .Append("{")
                .Append('"' + "name" + '"' + ":").Append(name)
                .Append("}")
                .Append("}");
            var result = HttpClientHelper.PostResponse<CreateGroupResult>(string.Format("https://api.weixin.qq.com/cgi-bin/groups/create?access_token={0}", access_token), builder.ToString());
            return result.errcode == 0;

        }


        /// <summary>
        /// 查询所有分组
        /// </summary>
        /// <param name="access_token">调用接口凭证</param>
        /// <returns></returns>
        public static GroupsJson GetGroups(string access_token)
        {
            var result = HttpClientHelper.PostResponse<GroupsJson>(string.Format("https://api.weixin.qq.com/cgi-bin/groups/get?access_token={0}", access_token), "");
            return result;
        }

        /// <summary>
        /// 查询用户所在分组
        /// </summary>
        /// <param name="access_token"></param>
        /// <param name="openid"></param>
        /// <returns>不为空时，表示查询成功，否则查询失败</returns>
        public static int GetUserGroup(string access_token, string openid)
        {
            var builder = new StringBuilder();
            builder.Append("{").Append('"' + "openid" + '"' + ":").Append(openid).Append("}");
            var result = HttpClientHelper.PostResponse<GetGroupIdResult>(string.Format("https://api.weixin.qq.com/cgi-bin/groups/getid?access_token={0}", access_token), builder.ToString());

            return result.groupid;
        }

        /// <summary>
        /// 修改分组名
        /// </summary>
        /// <param name="access_token">调用接口凭证</param>
        /// <param name="id">分组id，由微信分配</param>
        /// <param name="name">分组名字（30个字符以内）</param>
        /// <returns></returns>
        public static bool UpdateGroup(string access_token, string id, string name)
        {
            var builder = new StringBuilder();
            builder.Append("{")
                .Append('"' + "group" + '"' + ":")
                .Append("{")
                .Append('"' + "id" + '"' + ":").Append(id).Append(",")
                .Append('"' + "name" + '"' + ":").Append(name)
                .Append("}")
                .Append("}");

            var result = HttpClientHelper.PostResponse<WxJsonResult>(string.Format("https://api.weixin.qq.com/cgi-bin/groups/update?access_token={0}", access_token), builder.ToString());
            return result.errcode == 0;
        }


        /// <summary>
        /// 移动用户分组
        /// </summary>
        /// <param name="access_token">调用接口凭证</param>
        /// <param name="openid">用户唯一标识符</param>
        /// <param name="to_groupid">分组id</param>
        /// <returns></returns>
        public static bool MoveGroup(string access_token, string openid, string to_groupid)
        {
            var builder = new StringBuilder();
            builder.Append("{")
                .Append('"' + "openid" + '"' + ":").Append(openid).Append(",")
                .Append('"' + "to_groupid" + '"' + ":").Append(to_groupid)
                .Append("}");

            var result = HttpClientHelper.PostResponse<WxJsonResult>(string.Format("https://api.weixin.qq.com/cgi-bin/groups/members/update?access_token={0}", access_token), builder.ToString());
            return result.errcode == 0;
        }

        /// <summary>
        /// 设置备注名
        /// </summary>
        /// <param name="access_token">调用接口凭证</param>
        /// <param name="openid">用户唯一标识符</param>
        /// <param name="remark">新的备注名，长度必须小于30字符</param>
        /// <returns></returns>
        public static bool SetRemark(string access_token, string openid, string remark)
        {
            var builder = new StringBuilder();
            builder.Append("{")
                .Append('"' + "openid" + '"' + ":").Append(openid).Append(",")
                .Append('"' + "remark" + '"' + ":").Append(remark)
                .Append("}");

            var result = HttpClientHelper.PostResponse<WxJsonResult>(string.Format("https://api.weixin.qq.com/cgi-bin/user/info/updateremark?access_token={0}", access_token), builder.ToString());
            return result.errcode == 0;
        }

        /// <summary>
        /// 获取用户基本信息（包括UnionID机制）
        /// 注意：如果开发者有在多个公众号，或在公众号、移动应用之间统一用户帐号的需求，需要前往微信开放平台（open.weixin.qq.com）绑定公众号后，才可利用UnionID机制来满足上述需求。
        /// </summary>
        /// <param name="access_token"></param>
        /// <param name="openId"></param>
        /// <returns>UnionID机制的返回值中将包含“unionid”</returns>
        public static UserInfoResult GetInfo(string access_token, string openId)
        {
            UserInfoResult result = null;
            try
            {
                result = HttpClientHelper.GetResponse<UserInfoResult>(string.Format("https://api.weixin.qq.com/cgi-bin/user/info?access_token={0}&openid={1}&lang=zh_CN", access_token, openId));
            }
            catch (Exception)
            {

                throw;
            }

            if (result.errcode != ReturnCode.请求成功)
            {
                throw new Exception("错误" + result.errcode.ToString() + result.errmsg);
            }

            return result;
        }


        /// <summary>
        /// 获取关注者列表
        /// </summary>
        /// <param name="token"></param>
        /// <param name="nextOpenId">第一个拉取的OPENID，不填默认从头开始拉取</param>
        /// <returns></returns>
        public static OpenIdResultJson GetSubscribes(string access_token, string nextOpenId)
        {
            string url = string.Format("https://api.weixin.qq.com/cgi-bin/user/get?access_token={0}", access_token);
            if (!string.IsNullOrEmpty(nextOpenId))
            {
                url += "&next_openid=" + nextOpenId;
            }

            var result = HttpClientHelper.GetResponse<OpenIdResultJson>(url);
            return result;
        }



    }
}
